# 添加所需 R 包位置
#setwd("D:/projects/gitee/scrapyjd")
#.libPaths( unique(c( .libPaths(), "C:/Users/krup/Documents/R/win-library/3.5") ))
# 载入额外的 methods 包
#library(methods)
#source("D:/projects/gitee/rsjd/R/rsjd_funs.R", encoding = "UTF-8")
require(rsjd)
use("XML", "xml2", "RCurl", 
    "rvest", "stringr", "readxl", 
    "tidyverse", "scales", "lubridate", 
    "showtext", "RSelenium", "rvest")

system("java -jar C:/krupSoftware/bin/selenium-server-standalone-3.7.1.jar",wait = FALSE, invisible = FALSE)
remdr <- remoteDriver(remoteServerAddr = "localhost",
                      browserName = "firefox",
                      platform = "WINDOWS",
                      port = 4444L)

#remdr = hg_rs_remote_driver("firefox")
remdr
try({
  hg_rs_open(remdr)
  hg_rs_open(remdr)
  hg_rs_get_sessions_id(remdr)
}, TRUE)


# keywords = c("体温计", "暖奶器", "吸奶器", "奶瓶", "爬行垫", "血压计", "血氧仪")
# randomi = round(runif(1, 1, length(keywords)))
# keyword = paste0("婴儿 ", keywords[randomi])
# print(keyword)

# urls = hg_rs_jd_search_n_pages_urls(remdr, keyword , n_pages = 2)
# filename = paste0("c:/Users/krup/Documents/temp/", hg_save_filename(), "-",keyword,".csv")
# products = hg_rs_jd_products(remdr, keyword, filename,
#                                    n_pages = 1, n_products = 2)
# products

#hg_rs_jd_product_row(remdr, "https://item.jd.com/954843.html")


# "体温计",
keywords = c( "暖奶器", "吸奶器", "奶瓶", "爬行垫", "血压计", "血氧仪")
keywords2 = paste0("婴儿 ", keywords)

lapply(keywords2, function(w) {
  print(w)
  filename = paste0("c:/Users/krup/Documents/temp/", hg_save_filename(), "-",w,".csv")
  hg_rs_jd_products(remdr, w, filename,
                    n_pages = 5, n_products = 1050, max_products = 1050)
})

print("全部完成! GooD!!!")




